chore: eliminate sys.path.insert hacks — migrate to package-relative imports#272
chore: eliminate sys.path.insert hacks — migrate to package-relative imports#272sheepdestroyer wants to merge 6 commits into
Conversation
Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Revert 'from .circuit_breaker' to 'from router.circuit_breaker' in main.py: relative imports break when main.py is imported directly as 'main' (tests use 'from main import ...' with PYTHONPATH=.:router) - Add try/except ImportError fallback to 'from scripts.chat_helpers' across all 7 scripts (4 in scripts/, 3 in verification/) so they remain runnable via both package import and direct execution
…e imports This commit: - Removes sys.path.insert hacks from scripts/ - Adds router/__init__.py to make router a package - Updates router/main.py and router/agy_proxy.py to use package-aware imports - Updates tests and mocks to use absolute package paths - Updates CI workflow to use standard PYTHONPATH=. - Ensures both CLI and package execution modes work correctly Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Gemini Code Assist correctly identified that the PR's removal of sys.path hacks broke direct execution of all 9 scripts. When Python runs a script, it REPLACES sys.path[0] (the CWD) with the script's directory — so cross- package imports (router.*, scripts.chat_helpers) fail with ModuleNotFoundError. Two failure patterns, two fixes: 1. Scripts in scripts/ importing scripts.chat_helpers: try/except with bare 'from chat_helpers' fallback (found in sys.path[0]) 2. Scripts in scripts/verification/ importing scripts.chat_helpers or router.*: try/except with sys.path.insert to repo root (parents[2]) Verified: all 9 scripts now run successfully via both 'python scripts/X.py' (direct) and package imports (from scripts.X import ...)
There was a problem hiding this comment.
Sorry @sheepdestroyer, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
Warning Review limit reached
Next review available in: 46 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (22)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors imports across the codebase to use absolute package imports (e.g., prefixing with router.) and adds fallback mechanisms for script imports. The feedback recommends removing obsolete sys.path manipulation hacks in router/tests/test_dashboard_data.py and router/tests/test_resolve_external_urls.py that are no longer needed now that absolute package imports are used.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if router_path not in sys.path: | ||
| sys.path.insert(0, router_path) | ||
|
|
||
| import main | ||
| from router import main |
There was a problem hiding this comment.
Since the import has been migrated to the absolute package import from router import main, the sys.path.insert hack is no longer necessary. For package imports to resolve, Python looks at the parent directory of the package (the repository root), which is already automatically included in sys.path by the test runner (pytest). Adding router_path (which points to repo/router) to sys.path does not help resolve from router import main because the router package is not located inside the router directory itself. Removing this redundant block simplifies the code and aligns with the PR's goal of eliminating path insertion hacks. Note that the unused router_path variable on line 9 and the comment on line 8 can also be safely removed.
from router import main| if router_path not in sys.path: | ||
| sys.path.insert(0, router_path) | ||
|
|
||
| import main | ||
| from main import resolve_external_urls | ||
| from router import main | ||
| from router.main import resolve_external_urls |
There was a problem hiding this comment.
Since the imports have been migrated to absolute package imports (from router import main and from router.main import resolve_external_urls), the sys.path.insert hack is no longer necessary. For package imports to resolve, Python looks at the parent directory of the package (the repository root), which is already automatically included in sys.path by the test runner (pytest). Adding router_path (which points to repo/router) to sys.path does not help resolve from router import main because the router package is not located inside the router directory itself. Removing this redundant block simplifies the code and aligns with the PR's goal of eliminating path insertion hacks. Note that the unused router_path variable on line 7 and the comment on line 6 can also be safely removed.
| if router_path not in sys.path: | |
| sys.path.insert(0, router_path) | |
| import main | |
| from main import resolve_external_urls | |
| from router import main | |
| from router.main import resolve_external_urls | |
| from router import main | |
| from router.main import resolve_external_urls |
Gemini Code Assist flagged that test_dashboard_data.py and test_resolve_external_urls.py still use sys.path.insert(0, router_path) hacks despite having migrated to absolute package imports (from router import main). These hacks were doubly broken: 1. They inserted router/ (the package dir) into sys.path, which doesn't help resolve 'from router import main' — Python needs the PARENT dir. 2. router/tests/conftest.py already inserts the correct dir (repo root). Removed the dead sys/os imports along with the hack blocks. All 193 tests pass. Scripts import cleanly.
|
Closing in favor of a fresh PR with Gemini Code Assist review fixes applied. See #<new_PR>. |
|
Closed in favor of a fresh PR with all Gemini Code Assist review fixes applied. See #273. |
What
This PR removes vestigial
sys.path.inserthacks from scripts inscripts/, migrating to standard package-relative imports with ImportError fallbacks. It also adds a missing__init__.pyto therouter/directory, normalizes structured content parsing inchat_helpers.py, and fixes direct script execution after Gemini Code Assist review exposed latent import failures.Fixes #266
Changes since PR #271 (now closed)
ImportError fallbacks (9 scripts — Gemini Code Assist review fix):
When Python runs a script directly, it REPLACES sys.path[0] (the CWD) with the script's directory — so cross-package imports fail. Two patterns fixed:
scripts/importingscripts.chat_helpers: try/except with barefrom chat_helpersfallback (found in sys.path[0])scripts/verification/importingscripts.chat_helpersorrouter.*: try/except withsys.path.insertto repo root (parents[2])Verified: all 9 scripts run successfully via both
python scripts/X.py(direct) and package imports.Original PR #271 changes (preserved)
Import cleanup (9 scripts)
sys.path.inserthacks from all scripts inscripts/andverification/from scripts.chat_helpers import parse_chat_response+ ImportError fallbacktry/except ImportErrorpattern to all scripts — ensures they remain runnable via bothpython scripts/foo.py(direct) and package importsRouter package
router/__init__.pyto make router a proper Python packagerouter/main.pyto usefrom router.circuit_breaker import get_breaker(package-aware absolute import)from .circuit_breakerwas tested but reverted — it fails when test modules importmain.pydirectly viafrom main import ...withPYTHONPATH=.:routerchat_helpers.py
_normalize_chat_content()helper that handles structured content payloads from alt providers (lists of dicts, nested content, etc.)parse_chat_response()to use the new normalizer for bothcontentandreasoning_contentupgrade-prod.sh
.envvalidationMisc
asyncio,time) from test filesPreviously PR #271 — closed and reopened with review fixes applied.